<?php

class RegisterForgotForm extends User {

    const SCENARIO_ACTIVATION = 'activation';
    const SCENARIO_RESET_PASSWORD = 'reset_password';
    const SCENARIO_CHANGE_PASSOWRD = 'change_password';
    
    public function rules() {
	return array_merge(parent::rules(), array(
	    array('new_password, new_password_repeat', 'required', 
		'on' => array(
		    RegisterForgotForm::SCENARIO_ACTIVATION, 
		    RegisterForgotForm::SCENARIO_RESET_PASSWORD, 
		    RegisterForgotForm::SCENARIO_CHANGE_PASSOWRD),
		'message' => t('MSG_REQUIRED', 'messages')),
	     array('password', 'required', 
		'on' => RegisterForgotForm::SCENARIO_CHANGE_PASSOWRD,
		'message' => t('MSG_REQUIRED', 'messages')),
	    array('new_password', 'length',
		'min' => '6', 'tooShort' => t('MSG_STRING_TOO_SHORT', 'messages'),
		'max' => '30', 'tooLong' => t('MSG_STRING_TOO_LONG', 'messages')),
	    array('new_password_repeat', 'compare', 'compareAttribute' => 'new_password', 'message' => t('MSG_WRONG_CONFIRM_PASSWORD', 'messages')),
	    // Make new_password_repeat safe. If it doesn't appear in any other rule, it's considered not safe!
	    array('new_password_repeat', 'safe'),
	    array('securityCode', 'length', 'max' => '100', 'tooLong' => t('MSG_STRING_TOO_LONG', 'messages')),
	));
    }

    /**
     * Returns the static model of the specified AR class.
     * @return User the static model class
     */
    public static function model($className = __CLASS__) {
	return parent::model($className);
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
	return array_merge(parent::attributeLabels(), array(
	    'email' => t('LBL_EMAIL', 'user'),
	    'usernams' => t('LBL_USER_USERNAME', 'user'),
	    'password' => t('LBL_OLD_PASSWORD', 'user'),
	    'new_password' => t('LBL_NEW_PASSWORD', 'user'),
	    'new_password_repeat' => t('LBL_CONFIRM_PASSWORD', 'user'),
	));
    }

//    public function validateExistEmailAndInActive($attribute, $params) {
//        $criteria = new CDbCriteria();
//        $criteria->addCondition("email = :email");
//        $criteria->params = array(':email' => $this->$attribute);
//        $user = User::model()->find($criteria);
//
//        if ($user == null) {
//            $this->addError($attribute, t('MSG_NOT_EXISTED_EMAIL_CREATE','authorization'));
//        } else if ($user->active == 1) {
//            $this->addError($attribute, t('MSG_ACTIVATED_USER','authorization'));
//        }
//        
//    }
//    
//    public function validateForgotPassword($attribute, $params) {
//        $criteria = new CDbCriteria();
//        $criteria->addCondition("email = :email");
//        $criteria->params = array(':email' => $this->$attribute);
//        $user = User::model()->find($criteria);
//
//        if ($user == null) {
//            $this->addError($attribute, t('MSG_NOT_EXISTED_EMAIL_FORGOT','authorization'));
//        }
//        else if ($user->active == 0) {
//            $this->addError($attribute, t('MSG_NOT_ACTIVATED_USER','authorization'));
//        }
//    }
//    public function validateLogigearEmail($attribute, $params) {
//        $arr = explode('@', $this->$attribute);
//        $bool = FALSE;
//        if(isset($arr[1]) && $arr[1] == 'logigear.com')
//            $bool = TRUE;
//        if ( ! $bool) {
//            $this->addError($attribute, t('MSG_EMAIL_NOT_LOGIGEAR','authorization'));
//        }
//    }

    /**
     * Can not delete this funcion. It invoked with auth_assignments in parent function
     * @author Kien Tran <kien.tran@logigear.com>
     * 
     */
    protected function afterSave() {
	parent::afterSave();
    }

}

